package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.DynamicAllMessageBean;
import com.beiing.tianshuai.tianshuai.util.DateUtil;
import com.beiing.tianshuai.tianshuai.util.SpanStringUtils;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.qmuiteam.qmui.widget.QMUIRadiusImageView;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jinni on 2017/11/28 0028
 */

public class DynamicAllMessageAdapter extends BaseRecyclerViewAdapter<DynamicAllMessageBean.DataBean.NumberBean> {

    private final int ITEM_TYPE_PRAISE = 0;
    private final int ITEM_TYPE_REPLY = 1;

    private Context mContext;

    private OnItemClickListener mListener;

    public DynamicAllMessageAdapter(Context context) {
        mContext = context;
    }

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        DynamicAllMessageBean.DataBean.NumberBean bean = mList.get(position);
        if (TextUtils.isEmpty(bean.getCommentContent())) {
            return ITEM_TYPE_PRAISE;
        } else {
            return ITEM_TYPE_REPLY;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder result = null;
        View view;
        switch (viewType) {
            case ITEM_TYPE_PRAISE:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_notice_praise, parent, false);
                result = new PraiseViewHolder(view);
                break;
            case ITEM_TYPE_REPLY:
                view = LayoutInflater.from(mContext).inflate(R.layout.item_notice_comment, parent, false);
                result = new CommentViewHolder(view);
                break;
        }
        return result;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        DynamicAllMessageBean.DataBean.NumberBean bean = mList.get(position);
        if (holder instanceof PraiseViewHolder) {
            ((PraiseViewHolder) holder).setData(bean);
            ((PraiseViewHolder) holder).setListener(bean);
        } else if (holder instanceof CommentViewHolder) {
            ((CommentViewHolder) holder).setData(bean);
            ((CommentViewHolder) holder).setListener(bean);
        }
    }

    class PraiseViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.civ_avatar_praise)
        QMUIRadiusImageView civAvatarPraise;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_praise)
        TextView tvPraise;
        @BindView(R.id.iv_dynamic_pic)
        QMUIRadiusImageView ivDynamicPic;

        PraiseViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(DynamicAllMessageBean.DataBean.NumberBean bean) {
            String photo = bean.getPhoto();
            String name = bean.getName();
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .skipMemoryCache(false)
                    .centerCrop()
                    .override(350, 350);
            Glide.with(mContext)
                    .load(TextUtils.isEmpty(photo) ? R.mipmap.default_avatar
                            : HttpRequestConstant.USER_PIC_HEAD + photo)
                    .apply(requestOptions)
                    .into(civAvatarPraise);

            if (TextUtils.isEmpty(bean.getPicname())) {
                ivDynamicPic.setVisibility(View.GONE);
            } else {
                ivDynamicPic.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .asBitmap()
                        .load(HttpRequestConstant.DYNAMIC_PIC_HEAD + bean.getPicname())
                        .apply(requestOptions)
                        .into(ivDynamicPic);
            }
            tvName.setText(name);
            tvTime.setText(DateUtil.getTime(bean.getCreateTime()));
            if (!TextUtils.isEmpty(bean.getTableType())) {
                switch (bean.getTableType()) {
                    case "0":
                        tvPraise.setText("赞了这条动态");
                        break;
                    case "1":
                        tvPraise.setText("赞了这条评论");
                        break;
                    case "2":
                        tvPraise.setText("赞了这条动态");  //转发
                        break;
                    case "3":
                        tvPraise.setText("赞了这条评论");  //转发
                        break;default:
                }
            }
        }

        public void setListener(final DynamicAllMessageBean.DataBean.NumberBean bean) {
            // 头像的点击事件
            civAvatarPraise.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onAvatarClick(bean, getAdapterPosition());
                    }
                }
            });

            // item点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(bean, getAdapterPosition());
                    }
                }
            });
        }
    }

    class CommentViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.civ_avatar_comment)
        QMUIRadiusImageView civAvatarComment;
        @BindView(R.id.tv_name)
        TextView tvName;
        @BindView(R.id.tv_time)
        TextView tvTime;
        @BindView(R.id.tv_comment_content)
        TextView tvCommentContent;
        @BindView(R.id.iv_dynamic_pic)
        QMUIRadiusImageView ivDynamicPic;

        CommentViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }

        public void setData(DynamicAllMessageBean.DataBean.NumberBean bean) {
            String photo = bean.getPhoto();
            String name = bean.getName();
            RequestOptions requestOptions = new RequestOptions()
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .skipMemoryCache(false)
                    .centerCrop()
                    .override(350, 350);
            Glide.with(mContext)
                    .load(TextUtils.isEmpty(photo) ? R.mipmap.default_avatar
                            : HttpRequestConstant.USER_PIC_HEAD + photo)
                    .apply(requestOptions)
                    .into(civAvatarComment);

            if (TextUtils.isEmpty(bean.getPicname())) {
                ivDynamicPic.setVisibility(View.GONE);
            } else {
                ivDynamicPic.setVisibility(View.VISIBLE);
                Glide.with(mContext)
                        .asBitmap()
                        .load(HttpRequestConstant.DYNAMIC_PIC_HEAD + bean.getPicname())
                        .apply(requestOptions)
                        .into(ivDynamicPic);
            }
            tvName.setText(name);
            tvTime.setText(DateUtil.getTime(bean.getCreateTime()));
            if (!TextUtils.isEmpty(bean.getCommentContent())) {
                try {
                    String commentContentDecode = URLDecoder.decode(bean.getCommentContent(), "UTF-8");
                    tvCommentContent.setText(SpanStringUtils.getEmotionContent(mContext, tvCommentContent, commentContentDecode));
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
            }
        }

        public void setListener(final DynamicAllMessageBean.DataBean.NumberBean bean) {
            // 头像的点击事件
            civAvatarComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onAvatarClick(bean, getAdapterPosition());
                    }
                }
            });

            // item点击事件
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onItemClick(bean, getAdapterPosition());
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(DynamicAllMessageBean.DataBean.NumberBean bean, int position);

        void onAvatarClick(DynamicAllMessageBean.DataBean.NumberBean bean, int position);
    }
}
